package com.jwinf.cdLibary.controller;

import com.jwinf.cdLibary.domain.User;
import com.jwinf.cdLibary.tool.WindowUtils;


/**
 * @author jwinf
 *
 * 编辑用户窗口控制类
 */
public class EditUserFrameController extends OperateUserFrameController {

    // 重写submit方法 实现编辑操作
    @Override
    protected void submit() {
        // 获取组件中的数据
        String username = usertxt.getText();
        String pwd = pwdtxt.getText();
        String tel = teltxt.getText();
        String add = addtxt.getText();
        // 验证数据
        if (!check(username, "用户名为空") || !check(pwd, "密码为空") || !check(tel, "联系方式为空") || !check(add, "地址为空"))
            return;

        // 给user对象注入值
        user.setUsername(username);
        user.setPassword(pwd);
        user.setTel(tel);
        user.setAddr(add);
        // 判断是否符合提交条件
        if(pwdpass && reppass && telpass){
            // 调用服务层的编辑方法 并根据结果来弹出弹窗提示用户
            if(userService.edit(user)){
                WindowUtils.showTip("修改成功", "/icon/SuccessIcon.png", "-fx-background-color:  linear-gradient(to top, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
                return;
            }else {
                WindowUtils.showTip("修改失败", "/icon/warnIcon.png", "-fx-background-color:  linear-gradient(to top, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
            }
        }else {
            WindowUtils.showTip("请按规范填写相关信息", "/icon/warnIcon.png", "-fx-background-color:  linear-gradient(to top, #f3e7e9 0%, #e3eeff 99%, #e3eeff 100%);");
            return;
        }
    }

    // 初始化方法 往组件写入用户数据
    public void init(User user){
        this.user = user;
        usertxt.setText(user.getUsername());
        pwdtxt.setText(user.getPassword());
        reptxt.setText(user.getPassword());
        teltxt.setText(user.getTel());
        addtxt.setText(user.getAddr());
    }
}

